package com.ems.controller;

import com.ems.bo.ChargeDisRecordsBo;
import com.ems.config.LogPointer;
import com.ems.service.IBatteryBoxService;
import com.ems.service.IBatteryStatisticsDayService;
import com.ems.service.IStationService;
import com.ems.vo.BaseResult;
import com.ems.vo.BatteryBoxVo;
import com.ems.vo.BatteryStatisticsDayVo;
import com.ems.vo.StationVo;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 储能电站 前端控制器
 * </p>
 *
 * @author WuZH
 * @since 2023-10-24
 */
@RestController
@RequestMapping ( "/station" )
@CrossOrigin ( value = "*", maxAge = 3600 )
public class StationController {

    @Resource
    private IStationService iStationService;
    @Resource
    private IBatteryBoxService iBatteryBoxService;
    @Resource
    private IBatteryStatisticsDayService iBatteryStatisticsDayService;

    @RequestMapping ( "queryStationsOver" )
    @LogPointer ( "多站概览顶部数据" )
    public BaseResult queryStationsOver ( @RequestBody StationVo stationVo ) {
        //1.总电站数量
        Integer count = iStationService.queryCount ( stationVo );
        List<StationVo> stationVos = iStationService.queryList ( stationVo );
        //2.总装机功率
        double sumPower = stationVos.stream ().map ( StationVo :: getPower ).collect ( Collectors.toList () ).stream ().map ( Double :: valueOf ).collect ( Collectors.toList () ).stream ().mapToDouble ( po -> po ).sum ();
        //3.总装机容量
        double sumCapacity = stationVos.stream ().map ( StationVo :: getCapacity ).collect ( Collectors.toList () ).stream ().map ( Double :: valueOf ).collect ( Collectors.toList () ).stream ().mapToDouble ( capacity -> capacity ).sum ();
        //4.总充电量
        BatteryBoxVo batteryBoxVo = new BatteryBoxVo ();
        List<BatteryBoxVo> batteryBoxVos = iBatteryBoxService.queryList ( batteryBoxVo );
        double sumCharge = batteryBoxVos.stream ().map ( BatteryBoxVo :: getTotalChargeAmount ).collect ( Collectors.toList () ).stream ().map ( Double :: valueOf ).collect ( Collectors.toList () ).stream ().mapToDouble ( charge -> charge ).sum ();
        double sumDisCharge = batteryBoxVos.stream ().map ( BatteryBoxVo :: getTotalDischargeAmount ).collect ( Collectors.toList () ).stream ().map ( Double :: valueOf ).collect ( Collectors.toList () ).stream ().mapToDouble ( po -> po ).sum ();

        Map<String, Object> map = new HashMap<> ();
        map.put ( "count", count );
        map.put ( "sumPower", sumPower );
        map.put ( "sumCapacity", sumCapacity );
        map.put ( "sumCharge", sumCharge );
        map.put ( "sumDisCharge", sumDisCharge );
        return BaseResult.success ( map );
    }

    /**
     * [
     * {time:xxx, charge:xxx, disCharge:xxx},
     * {time:xxx, charge:xxx, disCharge:xxx},
     * {time:xxx, charge:xxx, disCharge:xxx}
     * ]
     *
     * @param batteryStatisticsDayVo
     * @return
     */
    @RequestMapping ( "chargeDisRecords" )
    @LogPointer ( "多站概览充放电记录" )
    public BaseResult chargeDisRecords ( @RequestBody BatteryStatisticsDayVo batteryStatisticsDayVo ) {
        List<BatteryStatisticsDayVo> batteryStatisticsDayVos = iBatteryStatisticsDayService.queryList ( batteryStatisticsDayVo );
        List<ChargeDisRecordsBo> chargeDisRecordsBos = new ArrayList<> ();
        for ( BatteryStatisticsDayVo statisticsDayVo : batteryStatisticsDayVos ) {
            ChargeDisRecordsBo chargeDisRecordsBo = new ChargeDisRecordsBo ();
            chargeDisRecordsBo.setTime ( statisticsDayVo.getDataTime () );
            chargeDisRecordsBo.setCharge ( statisticsDayVo.getTotalChargeCapacity () );
            chargeDisRecordsBo.setDisCharge ( statisticsDayVo.getTotalDisChargeCapacity () );
            chargeDisRecordsBos.add ( chargeDisRecordsBo );
        }
        return BaseResult.success ( chargeDisRecordsBos );
    }

    @RequestMapping ( "queryList" )
    @LogPointer ( "电站列表" )
    public BaseResult queryList ( @RequestBody StationVo stationVo ) {
        List<StationVo> stationVos = iStationService.queryList ( stationVo );
        return BaseResult.success ( stationVos, iStationService.queryCount ( stationVo ) );
    }

    @RequestMapping ( "saveSingle" )
    @LogPointer ( "保存电站" )
    public BaseResult saveSingle ( @RequestBody StationVo stationVo ) {
        stationVo = iStationService.saveSingle ( stationVo );
        return BaseResult.success ( stationVo );
    }

    /**
     * 1、所有电站列表
     * 2、如果没有指定电站Id则返回第一个电站，如果指定电站Id则返回指定电站信息
     *
     * @param stationVo
     * @return
     */
    @RequestMapping ( "singleSite" )
    @LogPointer ( "单站概况" )
    public BaseResult singleSite ( @RequestBody StationVo stationVo ) {
        Map<String, Object> map = iStationService.singleSite ( stationVo );
        return BaseResult.success ( map );
    }

    @RequestMapping ( "getStationList" )
    @LogPointer ( "查询所有电站" )
    public BaseResult getStationList ( @RequestBody StationVo stationVo ) {
        List<StationVo> stationList = iStationService.getStationList ( stationVo );
        return BaseResult.success ( stationList );
    }

    @RequestMapping ( "querySingle" )
    @LogPointer ( "查询单个电站" )
    public BaseResult querySingle ( @RequestBody StationVo stationVo ) {
        StationVo vo = iStationService.querySingle ( stationVo );
        return BaseResult.success ( vo );
    }

    @RequestMapping ( "updateSingle" )
    @LogPointer ( "修改电站" )
    public BaseResult updateSingle ( @RequestBody StationVo stationVo ) {
        StationVo vo = iStationService.updateSingle ( stationVo );
        return BaseResult.success ( vo );
    }

    /**
     * 故障管理-》报警列表
     *
     * @param stationVo
     * @return
     */
    @RequestMapping ( "queryStationAlarmList" )
    @LogPointer ( "查询电站报警列表" )
    public BaseResult queryStationAlarmList ( @RequestBody StationVo stationVo ) {
        List<StationVo> stationVos = iStationService.queryStationAlarmList ( stationVo );
        return BaseResult.success ( stationVos, iStationService.queryCount ( stationVo ) );
    }

}
